package com.c2soft.ncmxtradeX.controller.page;

import java.net.URI;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.log4j.Log4j2;

import org.codehaus.plexus.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import com.c2soft.ncmxtradeX.command.BrokeOrderCommand;
import com.c2soft.ncmxtradeX.util.JsonUtil;


@Log4j2
@Controller
public class CommonController {
	
	@Value("#{systemProperties['ncmxServer.redirect.url']}")
	private String baseServerUrl;
	
	@Autowired
	private RestTemplate restTemplate;
	
	@RequestMapping(value = "/tradeBook",method=RequestMethod.GET)
	
	public @ResponseBody List tradeBook(HttpServletRequest req) {
		URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
			    .path("tradeBook2")
			    .build()
			    .toUri();
		List response=restTemplate.getForObject(redirectUrl, List.class);
		String[] appendArray={"{\"rows\":",response.toString(),"}"};//TODO: clean junk
		return response;
	}

	@RequestMapping(value = "/countProfitLoss",method=RequestMethod.GET)

	public @ResponseBody List countProfitLoss(HttpServletRequest req) {
		
		URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
			    .path("ProfitLoss2")
			    .build()
			    .toUri();
		List response=restTemplate.getForObject(redirectUrl, List.class);
		String[] appendArray={"{\"rows\":",response.toString(),"}"};//TODO: clean junk
		return response;
	}


@RequestMapping(value = "/AccountDetailX",method=RequestMethod.GET)
	
	public @ResponseBody List accountDetailx(HttpServletRequest req) {
		
		URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
			    .path("broker/AccountDetailX")
			    .build()
			    .toUri();
		
		List response=restTemplate.getForObject(redirectUrl, List.class);
		String[] appendArray={"{\"rows\":",response.toString(),"}"};//TODO: clean junk
		return response;
	}
	
@RequestMapping(value = "/brokerAccountDetail",method=RequestMethod.GET)
	
	public @ResponseBody List accountDetail(HttpServletRequest req) {
		
		String brokerId=req.getParameter("brokerName");
		URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
			    .path("broker/brokerAccountDetail")
			    .queryParam("brokerId", brokerId)
			    .build()
			    .toUri();
		
		
		List response=restTemplate.getForObject(redirectUrl, List.class);
		String[] appendArray={"{\"rows\":",response.toString(),"}"};//TODO: clean junk
		return response;
	}

@RequestMapping(value = "/userAccountDetail",method=RequestMethod.GET)

public @ResponseBody List useraccountDetail(HttpServletRequest req) {
	String userId=req.getParameter("userName");
	String exchangeId=(String) req.getSession().getAttribute("trdUsrId");
	URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
		    .path("user/userAccountDetail")
		    .queryParam("userId", userId)
		    .build()
		    .toUri();
	
	List response=restTemplate.getForObject(redirectUrl, List.class);
	return response;
}

	@RequestMapping(value = "/netPosition",method={RequestMethod.GET,RequestMethod.POST})

	public @ResponseBody List netPosition(HttpServletRequest req,@RequestBody String json) {
		String brokerId=(String) req.getSession().getAttribute("trdUsrId");
		BrokeOrderCommand order= (BrokeOrderCommand) JsonUtil.fromJson(json,BrokeOrderCommand.class);
		order.setBrokeId(brokerId);
		URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
			    .path("netPosition2")
			    .queryParam("ptype", order.getPtype())
			    .build()
			    .toUri();
		List response=restTemplate.getForObject(redirectUrl, List.class);
		String[] appendArray={"{\"rows\":",response.toString(),"}"};//TODO: clean junk
		return response;
	}
	
	
@RequestMapping(value = "/prevtradeBook",method=RequestMethod.GET)
	
	public @ResponseBody List prevtradeBook(HttpServletRequest req) {
		URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
			    .path("prevtradeBook2")
			    .build()
			    .toUri();
		List response=restTemplate.getForObject(redirectUrl, List.class);
		String[] appendArray={"{\"rows\":",response.toString(),"}"};//TODO: clean junk
		
		return response;
	}

}
